package com.hht.tools.baselib

import android.graphics.drawable.Drawable
import android.widget.ImageView
import androidx.core.content.ContextCompat
import androidx.databinding.BindingAdapter


/**
 * Author:chen'hu
 * Time:2023/7/31 14:33
 * Description:This is ImageBindAdapter
 */
object ImageBindAdapter {
    @BindingAdapter("android:loadSrc")
    @JvmStatic //TODO 一定要加这句话，不然崩溃
    fun setSrc(view: ImageView, pkg: String) {
        val drawable = PackageManagerUtils.getDraw(pkg)
        drawable?.let {
            view.setImageDrawable(drawable)
        }
    }

    @BindingAdapter("android:loadSrc")
    @JvmStatic
    fun setSrc(view: ImageView, drawable: Drawable) {
        drawable?.let {
            view.setImageDrawable(drawable)
        }
    }

    @BindingAdapter("android:loadSrc")
    @JvmStatic
    fun setSrc(view: ImageView, iconResId: Int) {
        val drawable = ContextCompat.getDrawable(view.context, iconResId)
        drawable?.let {
            view.setImageDrawable(drawable)
        }
    }
}